<?php

namespace app\admin\controller;

use app\admin\model\AdminLog;
use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\Hook;
use think\Validate;

/**
 * 后台首页
 * @internal
 */
class Huodong extends Backend
{

    protected $noNeedLogin = ['login'];
    protected $noNeedRight = ['index', 'logout'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
    }

    public function index()
    {

        if (isset($_GET['code'])){$code=$_GET['code'];}else{$code="";}

        $res=Db::query("select * from fa_huodong  where concat(title) like '%$code%'");
        //=渲染列表================================================================================================================
        $len=15;   //每页长度
        $num=count($res);//总数
        $page=ceil($num/$len);

        if (isset($_GET['now_page'])) {$now_page=$_GET['now_page'];}else{$now_page=1;} //设定当前页
        $last_page=$now_page-1; if ($last_page<1){$last_page=1;}//上一页
        $next_page=$now_page+1;if ($next_page>$page){$next_page=$page;}//下一页
        $limit=($now_page-1)*$len;

        $res1=Db::query("select * from fa_huodong  where concat(title) like '%$code%' order by id desc ");

//        dump($user_res);
        $this->assign("now_page",$now_page);
        $this->assign("last_page",$last_page);
        $this->assign("next_page",$next_page);
        $this->assign("res",$res1);
        $this->assign("num",$num);
        $this->assign("page",$page);
        $this->assign("len",$len);
        //分页查询============================================================================================================


     //   print_r($res1);


        return $this->fetch("index");
    }

    public function add()
    {


        if ($_SERVER['REQUEST_METHOD']=="POST")
        {



           $data=$_POST['data'];
           $data['add_time']=time();


           $len=count(explode(",",$data['doctor_ids']));

            for($i=0;$i<$len;$i++)
            {
                $a="doctor_id"."[".$i;
              unset($data[$a]);
            }



         $res= Db::name("huodong")->insert(array(
             'title'=>$data['title'],
              'start_time'=>$data['start_time'],
             'end_time'=>$data['end_time'],
             'addr'=>$data['addr'],
             'store_id'=>$data['store_id'],
             'add_time'=>$data['add_time'],
             'doctor_ids'=>$data['doctor_ids'],
             'price'=>$data['price']
         ));

       //  print_r($res);

        }else{

            //获取门店
            $res=Db::query("select * from fa_shopro_store where status='1'");
            //获取全部医生
            $res1=Db::query("select * from fa_doctor ");
            $this->assign(array(
                'mendian_res'=>$res,
                'doctor_res'=>$res1
            ));

          //  print_r($res1);
            return $this->fetch("add");
        }

    }

    /*
       * 活动报名列表
       * */
    public function user_list()
    {
        $code='';
        if (isset($_GET['code']))
        {
            $code=$_GET['code'];
        }
        $huodong_id=$_GET['id'];

        $res=Db::query("select * from fa_huodong_baoming where concat(name,phone) like '%$code%' and  huodong_id='$huodong_id' order  by id desc ");
        $this->assign(array(
            'res'=>$res,
            'id'=>$huodong_id
        ));

        return $this->fetch("user_list");
    }

    public function upd()
    {


        if ($_SERVER['REQUEST_METHOD']=="POST")
        {

            $data=$_POST['data'];
            $id=$data['id'];
            unset($data['id']);

            $len=count(explode(",",$data['doctor_ids']));
            for($i=0;$i<$len;$i++)
            {
                $a="doctor_id"."[".$i;
                unset($data[$a]);
            }
            $new_=array_unique(explode(",",$data['doctor_ids']));

            $res= Db::name("huodong")->where(array(
                'id'=>$id
            ))->update(array(
                'title'=>$data['title'],
                'start_time'=>$data['start_time'],
                'end_time'=>$data['end_time'],
                'addr'=>$data['addr'],
                'store_id'=>$data['store_id'],

                'doctor_ids'=>implode(",",$new_),
                'price'=>$data['price']
            ));

          //  print_r($res);

        }else{
            $id=$_GET['id'];
            $res1=Db::query("select * from fa_huodong where id='$id'");
            $list=[];
            foreach ($res1 as $val1)
            {
                $title=$val1['title'];
                $start_time=$val1['start_time'];
                $end_time=$val1['end_time'];
                $addr=$val1['addr'];
                $store_id=$val1['store_id'];
                $res2=Db::query("select * from fa_shopro_store where id='$store_id'");
                $store_name='';
                foreach ($res2 as $val2)
                {
                    $store_name=$val2['name'];
                }
                $add_time=$val1['add_time'];
                $doctor_ids=explode(",",$val1['doctor_ids']);
                $price=$val1['price'];
                $arr=[
                    'id'=>$id,
                    'title'=>$title,
                    'start_time'=>$start_time,
                    'end_time'=>$end_time,
                    'addr'=>$addr,
                    'store_name'=>$store_name,
                    'store_id'=>$store_id,
                    'add_time'=>$add_time,
                    'doctor_ids'=>$doctor_ids,
                    'price'=>$price,
                    'doctor_len'=>count($doctor_ids)
                ];
                array_push($list,$arr);
            }

           //  print_r($list);
            //获取门店
            $res=Db::query("select * from fa_shopro_store where status='1'");

            //获取全部医生
            $res3=Db::query("select * from fa_doctor ");

            $this->assign(array(
                'mendian_res'=>$res,
                'info'=>$list,
                'doctor_res'=>$res3
            ));

            return $this->fetch("upd");
        }

    }




    /*
 * 批量删除
 * */
    public function delHuodong()
    {

        if ($_SERVER['REQUEST_METHOD']=="POST")
        {
            print_r($_POST);
            $ids=$_POST['ids'];
            $id_len=count($ids);
            for ($i=0;$i<$id_len;$i++)
            {
                $id=$ids[$i];
                $res= Db::execute("delete from fa_huodong where id='$id'");
            }

        }

    }

    public function delBaoming()
    {

        if ($_SERVER['REQUEST_METHOD']=="POST")
        {
         //   print_r($_POST);
            $ids=$_POST['ids'];
            $id_len=count($ids);
            for ($i=0;$i<$id_len;$i++)
            {
                $id=$ids[$i];
                $res= Db::execute("delete from fa_huodong_baoming where id='$id'");
            }

        }

    }


}